using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Administration_Toolkit_v2
{
    public partial class ModifyApplicationCultures : Form
    {
        public ModifyApplicationCultures()
        {
            InitializeComponent();
        }

        private void ModifyApplicationCultures_Load(object sender, EventArgs e)
        {
            cmbApplications.DataSource = Utilities.GetApplicationList();
            cmbApplications.ValueMember = "ApplicationID";
            cmbApplications.DisplayMember = "ApplicationName";

            lbApplicationCultures.DataSource  = Utilities.GetApplicationCultureList(cmbApplications.Text);
            lbApplicationCultures.ValueMember="CultureID"; 
            lbApplicationCultures.DisplayMember = "CultureName";
              

            lbCultures.DataSource =Utilities.GetApplicationNonCultureList(cmbApplications.Text);
            lbCultures.ValueMember="CultureID"; 
            lbCultures.DisplayMember = "CultureName";
             

        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void cmbApplications_SelectedIndexChanged(object sender, EventArgs e)
        {
            lbApplicationCultures.DataSource = Utilities.GetApplicationCultureList(cmbApplications.Text);
            lbApplicationCultures.ValueMember = "CultureID";
            lbApplicationCultures.DisplayMember = "CultureName";
            

            lbCultures.DataSource = Utilities.GetApplicationNonCultureList(cmbApplications.Text);
            lbCultures.ValueMember = "CultureID";
            lbCultures.DisplayMember = "CultureName";
            
        }

        private void btnAddCulture_Click(object sender, EventArgs e)
        {
            try
            {


                if (lbCultures.Text.Trim() == "All")
                    {
                        foreach (DataRowView tmpCulture in lbCultures.Items)
                                {
                                    if (tmpCulture[1].ToString().Trim()!= "All")
                                    {
                                        Utilities.CreateNewApplicationCulture(cmbApplications.Text, tmpCulture[1].ToString());
                                    }
                                }

                    }
                    else
                    {
                        Utilities.CreateNewApplicationCulture(cmbApplications.Text, lbCultures.Text);
                    }
                        lbApplicationCultures.DataSource = Utilities.GetApplicationCultureList(cmbApplications.Text);
                        lbApplicationCultures.ValueMember = "CultureID";
                        lbApplicationCultures.DisplayMember = "CultureName";
                        

                        lbCultures.DataSource = Utilities.GetApplicationNonCultureList(cmbApplications.Text);
                        lbCultures.ValueMember = "CultureID";
                        lbCultures.DisplayMember = "CultureName";
                    
                    txtStatus.Text="Culture Added"; 
                
            }
            catch (Exception ex)
            {
                txtStatus.Text = ex.Message; 
            }
        }

        private void btnDeleteCulture_Click(object sender, EventArgs e)
        {

            try
            {


                if (lbApplicationCultures.Text.Trim() == "All")
                    {
                        foreach (DataRowView tmpCulture in lbApplicationCultures.Items)
                        {
                            if (tmpCulture[1].ToString().Trim() != "All")
                            {
                             Utilities.DeleteApplicationCulture(cmbApplications.Text, tmpCulture[1].ToString());
                            }
                        }

                    }
                    else
                    {
                        Utilities.DeleteApplicationCulture(cmbApplications.Text, lbApplicationCultures.Text);
                    }
                    lbApplicationCultures.DataSource = Utilities.GetApplicationCultureList(cmbApplications.Text);
                    lbApplicationCultures.ValueMember = "CultureID";
                    lbApplicationCultures.DisplayMember = "CultureName";


                    lbCultures.DataSource = Utilities.GetApplicationNonCultureList(cmbApplications.Text);
                    lbCultures.ValueMember = "CultureID";
                    lbCultures.DisplayMember = "CultureName";

                    txtStatus.Text = "Culture Deleted";
                }
            
            catch (Exception ex)
            {
                txtStatus.Text = ex.Message;
            }
        }
    }
}